/**
 * @date   2011-5-8
 * @author tianye
 */
package cn.edu.dlut.WingGame.IOsystem;

import android.view.MotionEvent;
import cn.edu.dlut.WingGame.BaseType.ObjectState;
import cn.edu.dlut.WingGame.Object.WingObject;
import cn.edu.dlut.WingGame.Tools.ObjectContainer;

public class WingTouch {
	public static boolean isTouchDown = false;
	public static float X;
	public static float Y;
	public static MotionEvent mEvent;
	private static ObjectContainer mObjects = new ObjectContainer();

	public static boolean onTouchEvent(MotionEvent event) { // 触笔事件处理
		mEvent = event;
		if (event.getAction() == MotionEvent.ACTION_DOWN)
			isTouchDown = true;
		else if (event.getAction() == MotionEvent.ACTION_UP)
			isTouchDown = false;
		X = event.getX();
		Y = event.getY();

		return postMessage(event);
	}

	public static void addTouchListener(WingObject obj) {
		mObjects.addObject(obj);
	}

	public static void delTouchListener(int id) {
		mObjects.delObjectByID(id);
	}

	public static void delTouchListener(String name) {
		WingObject obj;
		for (int i = 0; i < mObjects.getSize(); i++) {
			obj = mObjects.getObject(i);
			if (obj.mName == name) {
				mObjects.delObjectByID(obj.mID);
				break;
			}
		}
	}

	private static boolean postMessage(MotionEvent event) {
		WingObject obj;
		for (int i = 0; i < mObjects.getSize(); i++) {
			obj = mObjects.getObject(i);
			if (obj.getState() != ObjectState.stop)
				((TouchListener) obj).onTouchEvent(event);
		}
		return true;
	}

}
